import Reflux from "reflux"
import GUtils from "@/utils";
import BaseAction from "@/base/BaseAction";

const prefix ="/shop/shop/";
const api = {
    search: prefix+"search",
    create: prefix+"create",
    edit: prefix+"edit",
    detail:  prefix+"detail",
    enabled:  prefix+"enabled",
    buildQRImg:  prefix+"buildQRImg"
};

const createAction = () => {
    const Action = Reflux.createActions({
        ...BaseAction,
        enabled:{asyncResult:true},
        doMapPos:{},//进行定位
        buildQRImg:{asyncResult:true},
    });

    Action.search.listen(function (so) {
        GUtils.jsonAjax.post(api.search, so).then(this.completed, this.failed);
    });



    Action.getDetail.listen(function (id,callback) {
        GUtils.Ajax.post(api.detail, {id}).then(this.completed.bind(this,callback), this.failed);
    });
    Action.create.listen(function (vo) {
        GUtils.jsonAjax.post(api.create, vo).then(this.completed.bind(this), this.failed);
    });
    Action.edit.listen(function (vo) {
        GUtils.jsonAjax.post(api.edit, vo).then(this.completed.bind(this), this.failed);
    });
    Action.enabled.listen(function (id,enabled) {
        GUtils.Ajax.post(api.enabled, {id, enabled}).then(this.completed, this.failed);
    });
    Action.buildQRImg.listen(function (id,callback) {
        GUtils.fileAjax({
            method: 'post',
            url: GUtils.root+"/web" + api.buildQRImg,
            params: {id},
            callback: callback
        });
    });
    return Action;
};

export {api, createAction}